package leetcode.list;

import lombok.val;
import utils.node.ListNode;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description: https://leetcode.cn/problems/remove-duplicate-node-lcci/description/
 * @Author Ammar
 * @Create 2023/9/14 18:44
 */
public class _面试题_02_01_移除重复节点 {

    public ListNode removeDuplicateNodes(ListNode head) {
        ListNode node = head;
        while (node != null) {
            ListNode node1 = node;
            while (node1.next != null) {
                if (node1.next.val == node.val) {
                    node1.next = node1.next.next;
                } else {
                    node1 = node1.next;
                }
            }
            node = node.next;
        }
        return head;
    }

    public ListNode removeDuplicateNodes1(ListNode head) {
        if (head == null) return null;
        Set<Integer> set = new HashSet<>();
        ListNode node = head;
        set.add(node.val);
        while (node != null && node.next != null) {
            if (set.contains(node.next.val)) {
                node.next = node.next.next;
            } else {
                set.add(node.next.val);
                node = node.next;
            }
        }
        return head;
    }
}
